package org.shijiagen.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.shijiagen.param.ProductHotParam;
import org.shijiagen.param.ProductIdParam;
import org.shijiagen.param.ProductIdsParam;
import org.shijiagen.param.ProductPromoParam;
import org.shijiagen.pojo.Product;
import org.shijiagen.product.service.ProductService;
import org.shijiagen.utils.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @PostMapping("promo")
    public R promo(@RequestBody @Validated ProductPromoParam param){
        return productService.promo(param.getCategoryName()) == null ? R.fail("该类别找不到！") : R.ok("结果返回成功！",productService.promo(param.getCategoryName()).getRecords());
    }

    @PostMapping("hots")
    public R hots(@RequestBody @Validated ProductHotParam productHotParam){
        Page<Product> hotsProduct = productService.hots(productHotParam);
        return hotsProduct == null ? R.fail("数据查询失败！"):R.ok("多类别热门商品查询成功！",hotsProduct.getRecords());
    }

    @PostMapping("category/list")
    public R categoryList(){
        return productService.categoryList();
    }

    @PostMapping("bycategory")
    public R byCategory(@RequestBody @Validated ProductIdsParam productIdsParam){
        Page<Product> productPage = productService.byCategory(productIdsParam);
        return R.ok("查询成功！",productPage.getRecords(),productPage.getTotal());
    }

    @PostMapping("all")
    public R all(@RequestBody @Validated ProductIdsParam productIdsParam){
        Page<Product> productPage = productService.byCategory(productIdsParam);
        return R.ok("查询成功！",productPage.getRecords(),productPage.getTotal());
    }

    @PostMapping("detail")
    public R detail(@RequestBody @Validated ProductIdParam productIdParam){
        return R.ok(productService.detail(productIdParam.getProductID()));
    }

    @PostMapping("pictures")
    public R pictures(@RequestBody @Validated ProductIdParam productIdParam){
        return R.ok(productService.pictures(productIdParam.getProductID()));
    }


}
